
# # #封装excel类，用于操作excel文件，管理数据并CRUD（增删改查）
# import xlrd
# import xlwt
# #用于副本操作
# from xlutils.copy import copy
# import os
# class Excel(object):
#     def __init__(self,path):
#         self.path=path

#     def read_all_data(self,sname):
#         #1.1首先要拿到当前excel表的上下文
#         wookbook=xlrd.open_workbook(self.path)
#         content=wookbook.sheet_by_name(sname)
#         return content
#     def write_row_data(self,obj,sname):#写的内容，sheet页
#         #2.1首先要获取当前表的总行数
#         #2.2获取当前指定sheet
#         #2.3copy
#         #2.4 修改副本内容
#         #2.5 save
#         #2.6打印信息
#         pass
#     def write_cell_data(self,obj,sname):
#         #3.1获取当前的sheet
#         #3.2 copy
#         #3.3 get_sheet(0)
#         #3.4 循环查找当前的sheet内容
#         #3.4.1 对obj的指定内容，进行匹配
#         #3.4.1.1 如果找到了 就修改 msg=ok
#         #3.4.1.2 没有找到 msg=error
#         #3.5 save
#         #3.6 return msg
#         pass
#     def del_all_data(self):
#         pass
#     def del_row_data(self):
#         pass
# if __name__=='__main__':
#     #path __file__
#     #动态获取路径
#     d = os.path.dirname(__file__)#获取当前执行的py文件的目录
#     #获取当前py文件执行时候的父级目录
#     parent_path = os.path.dirname(d)
#     file_path = parent_path + '/data/user.xls'
#     excel=Excel(file_path)#路径不允许写相对路径
#     res=excel.read_all_data('user')
#     print(res)
#     print(res.name,res.nrows,res.ncols)
#     print(res.cell(0,0).value)
# #建议obj传入一个字典


import xlrd,xlwt
from xlutils.copy import copy
import os 
class Excel(object):
    def __init__(self, path):
        self.path = path
    #excel的读取,传入参数:你要读取表的名字
    def find_data(self, sname):
        find_rd=xlrd.open_workbook(self.path)
        sheet=find_rd.sheet_by_name(sname)
        for i in range(sheet.nrows):
            values=sheet.row_values(i)
            for j in values:
                print(j,end=' '*6)
            print('')
    #excel的添加,传入参数:你要添加的数据—列表格式
    def add_data(self,slist):
        add_wt=xlrd.open_workbook(self.path)
        row=add_wt.sheets()[0].nrows
        new_add_wt=copy(add_wt)
        sheet=new_add_wt.get_sheet(0)
        i=0
        for j in slist:
            sheet.write(row,i,j)
            i+=1
        new_add_wt.save(self.path)

if __name__ == "__main__":
    d = os.path.dirname(__file__)
    print(d)
    parent_path = os.path.dirname(d)
    print(parent_path)
    file_path = parent_path + '/data/goods_info.xls'
    excel = Excel(file_path)
    list_goods=['36','小耳环','149','60','11111']
    excel.add_data(list_goods)
    excel.find_data('sheet1')



